<template>
  <div>
    <!-- 
          el-dialog
          center	是否对头部和底部采用居中布局	boolean
       -->
    <el-dialog
      center
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
    >
      <el-form :model="role_info" ref="roleInfo" :rules="rules">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="role_info.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth" prop="pid">
          <el-tree
            :data="menulist"
            show-checkbox
            node-key="id"
            :props="defaultProps"
            default-expand-all
            ref="treeRef"
          >
          </el-tree>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="role_info.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRoutes } from "../../router/index";
import { roleAdd, roleEdit, roleInfoa } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      rules: {
        rolename: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      role_info: {
        rolename: "",
        menus: "",
        status: 1,
      },
      defaultProps: {
        children: "children",
        label: "title",
      },
      formLabelWidth: "120px",
      indexRoutes: indexRoutes,
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getmenus",
    }),
  },

  mounted() {
    this.getMenu();
    
  },
  methods: {
    getHalfCheckedKeys(e) {
      console.log(e);
    },
    
    ...mapActions({
      getMenu: "menu/getMenuList",
      getRole: "role/getRoleList",
    }),
    // 清空表单
    reset() {
     this.role_info={
        rolename: "",
        menus: "",
        status: 1,
      },
      this.$refs.roleInfo.resetFields();
      this.$refs.treeRef.setCheckedKeys([])
    },
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    // 封装一个事件
    lookup(id) {
      console.log(id, 111111111111111);
      roleInfoa({ id: id }).then((res) => {
        console.log(res);
        this.role_info = res.list;
        this.role_info.id = id;
        console.log(this.role_info);
        this.$refs.treeRef.setCheckedKeys(this.role_info.menus?this.role_info.menus.split(','):[])
      });
    },
    add() {
      console.log(this.role_info);
      this.$refs.roleInfo.validate((val) => {
        if (val) { 
          this.role_info.menus=this.$refs.treeRef.getCheckedKeys().join(',')
          if (this.isAdd) {
            // c菜单添加
            
            roleAdd(this.role_info).then((res) => {
              
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.getRole();
                 this.cancel();
              } else {
                this.$message.error(res.msg);
              }
             
            });
          } else {
            // 菜单编辑setCheckedKeys//getCheckedKeys
          // console.log(this.$refs.treeRef.getCheckedKeys(),3333333333333333333)
            
          
            roleEdit(this.role_info).then((res) => {
              console.log(res);
              if (res.code == 200) {
                 
                this.$message.success(res.msg);
                this.getRole();
               this.cancel()
                // this.role_info.menus.split('')
              } else {
                this.$message.error(res.msg);
              }
              this.cancel();
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
